package com.powernode.bank.web;

import com.powernode.bank.exception.MoneyNotEnoughException;
import com.powernode.bank.exception.TransferException;
import com.powernode.bank.service.AccountService;
import com.powernode.bank.service.impl.AccountServiceImpl;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/transfer")
public class AccountServlet extends HttpServlet {


   private AccountService accountService= new AccountServiceImpl();
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String fromActno = request.getParameter("fromActno");
        String toActno = request.getParameter("toActno");
        String money = request.getParameter("money");


        try {
            accountService.transfer(fromActno,toActno,Double.parseDouble(money));
            response.sendRedirect(request.getContextPath()+"/success.html");
        } catch (MoneyNotEnoughException e) {

            request.setAttribute("msg",e.getMessage());
            request.getRequestDispatcher("/error1.html").forward(request,response);
        } catch (TransferException e) {
            request.setAttribute("msg",e.getMessage());
            request.getRequestDispatcher("/error2.html").forward(request,response);
        }

    }
}
